using Newtonsoft.Json;
using SmartLink.Application.IServices;
using SmartLink.Application.Services;
using SmartLink.Domain.Entity;
using SmartLink.DTOS;
using SmartLink.DTOS.Account;
using SmartLink.EnumLibrary;
using SmartLink.Infrastructure;
using SmartLink.Infrastructure.Cache;
using SmartLink.WebApi.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace SmartLink.WebApi.Areas.Mobile
{

    /// <summary>
    /// 小程序端 账号相关
    /// </summary>

    [RoutePrefix("api/mobile/Account")]
    public class AppAccountController : BaseAPIControl
    {
        private IAccountAppServices _IAccountAppServices;
        public AppAccountController(IAccountAppServices iAccountAppServices)
        {
            _IAccountAppServices = iAccountAppServices;
        }


        #region  验证码

        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <param name="phoneNumber"></param>
        /// <returns></returns>

        [HttpPost]
        [Route("SendValidateCode")]
        public Results SendValidateCode(PhoneDTO phoneNumber)
        {
            string openId = CurrentUser.Current.OpenID;
            string reMsg = _IAccountAppServices.SendValidateCode(openId, phoneNumber.PhoneNumer);
            bool successFlag = reMsg.Length>0?false:true;
            if (successFlag)
                return InfrastructureAppService.Response();
            else
                return InfrastructureAppService.Response(reMsg, StateCodeEnum.失败);
        }

        /// <summary>
        /// 确认验证码
        /// </summary>
        /// <param name="ComfirmCode"></param>
        /// <returns></returns>

        [HttpPost]
        [Route("ComfirmCode")]
        public Results ComfirmCode(ValidateCodeDTO codeDto)
        {
            string openId = CurrentUser.Current.OpenID;
            return _IAccountAppServices.ComfirmCode(openId, codeDto);
        }

        #endregion


        #region 小程序登录

        /// <summary>
        /// 微信授权登录
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>

        [AllowAnonymous]
        [HttpPost]
        [Route("WXAuthorization")]
        public Results<WXLoginResponseDTO> WXAuthorization(WXAuthorizationRequestDTO requestDTO)
        {
            return _IAccountAppServices.WXAuthorization(requestDTO);
        }

        #endregion


        #region  用户车牌管理

        /// <summary>
        /// 用户绑定车牌
        /// </summary>
        /// <param name="plateDto"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("BindVehiclePlate")]
        public Results BindVehiclePlate(PlateDto plateDto)
        {
            return _IAccountAppServices.BindVehiclePlate(CurUserId, plateDto);
        }

        /// <summary>
        /// 获取用户绑定的车牌
        /// </summary>
        /// <param name="curUserId"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetUserVehiclePlates")]
        public Results<IList<UserToPlateDTO>> GetUserVehiclePlates()
        {
            return _IAccountAppServices.GetUserVehiclePlates(CurUserId);
        }

        /// <summary>
        /// 获取用户绑定车牌状态
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetPlateBindStatus")]
        public Results<bool> GetPlateBindStatus()
        {
            return _IAccountAppServices.GetPlateBindStatus(CurUserId);
        }

        /// <summary>
        /// 设置用户默认车牌
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("SetDefaultPlate")]

        public Results SetDefaultPlate(DefaultPlateDTO plateIdDTO)
        {
            return _IAccountAppServices.SetDefaultPlate(CurUserId, plateIdDTO);
        }

        /// <summary>
        /// 删除用户车牌
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("DeletePlate")]
        public Results DeletePlate(DefaultPlateDTO plateIdDTO)
        {
            return _IAccountAppServices.DeletePlate(CurUserId, plateIdDTO);
        }

        #endregion

    }
}
